﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using BUS;
namespace GUI
{
    public partial class frmConfig : Form
    {
        public frmConfig()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Configuration _config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            _config.AppSettings.Settings["Server"].Value = txtServer.Text;
            _config.AppSettings.Settings["Username"].Value = txtSa.Text;
            _config.AppSettings.Settings["Password"].Value = txtPass.Text;
            if (checkBox1.Checked)
            {
                _config.AppSettings.Settings["Integrated"].Value = "false";
            }
            else
            {
                _config.AppSettings.Settings["Integrated"].Value = "true";
            }
            _config.Save(ConfigurationSaveMode.Modified);
            ConfigurationManager.RefreshSection("appSettings");
            try
            {
                KhuVucBus.DanhSachKhuVuc();
                _config.AppSettings.Settings["Integrated"].Value = "false";
                this.Dispose();
                
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối đến CSDL !", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            
        }

        private void frmConfig_Load(object sender, EventArgs e)
        {         
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                txtPass.Enabled = true;
                txtSa.Enabled = true;
            }
            else
            {
                txtPass.Enabled = false;
                txtSa.Enabled = false;
            }
        }

        private void frmConfig_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
                KhuVucBus.DanhSachKhuVuc();
                this.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không thể kết nối đến CSDL !", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
